//
//  AddImgCell.m
//  收纳
//
//  Created by  on 2022/10/28.
//

#import "AddImgCell.h"
#import "AddCell.h"

@interface AddImgCell()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end
@implementation AddImgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[AddCell nib] forCellWithReuseIdentifier:[AddCell ID]];
}
- (void)setModel:(Goods *)model{
    _model = model;
    [self.collectionView reloadData];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:model.imgs.count inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.imgs.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AddCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[AddCell ID] forIndexPath:indexPath];
    cell.btn.hidden = indexPath.row == self.model.imgs.count;
    cell.img.image = indexPath.row == self.model.imgs.count ? [UIImage imageNamed:@"添加_白"]:[UIImage imageWithData:self.model.imgs[indexPath.row]];
    WS(self)
    cell.block = ^{
        [weakself.model.imgs removeObjectAtIndex:indexPath.row];
        [weakself.collectionView reloadData];
    };
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.model.imgs.count) {
        if (self.block) {
            self.block();
        }
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(110,110);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

@end
